<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试修改</title>
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>
<body>
<style>
	* {
		font-size: 12px;
		padding:0px;
		margin:0px;
	}
</style>

<!-- 添加 -->
<script type="text/javascript">

	var editIndex_editPurchPlan = undefined;
	
	//结束编辑行
	function endEditing_editPurchPlan(){
		
		if (editIndex_editPurchPlan == undefined){return true}
		
		if ($('#yzr-editPurchPlan-dg').datagrid('validateRow', editIndex_editPurchPlan)){
			
			//给药品名赋值
			var ed = $('#yzr-editPurchPlan-dg').datagrid('getEditor', {index:editIndex_editPurchPlan,field:'medId'});					
			var medName = $(ed.target).combogrid('getText');			
			$('#yzr-editPurchPlan-dg').datagrid('getRows')[editIndex_editPurchPlan]['medName'] = medName;
			
			//给供应商名赋值
			var ed2 = $('#yzr-editPurchPlan-dg').datagrid('getEditor', {index:editIndex_editPurchPlan,field:'supId'});					
			var supName = $(ed2.target).combogrid('getText');			
			$('#yzr-editPurchPlan-dg').datagrid('getRows')[editIndex_editPurchPlan]['supName'] = supName;
			
			
			
			$('#yzr-editPurchPlan-dg').datagrid('endEdit', editIndex_editPurchPlan);			
			editIndex_editPurchPlan = undefined;
			
			
			return true;
		} else {
			return false;
		}
	}
	
	//单击行
	function onClickRow_editPurchPlan(index, field){
		
		if (editIndex_editPurchPlan != index){
			
			if (endEditing_editPurchPlan()){			
				$('#yzr-editPurchPlan-dg').datagrid('selectRow', index).datagrid('beginEdit', index);
				
				/* var ed = $('#yzr-editPurchPlan-dg').datagrid('getEditor', {index:index,field:field});	
				if (ed){
					($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
				} */								
				editIndex_editPurchPlan = index;
				
			} else {			
				$('#yzr-editPurchPlan-dg').datagrid('selectRow', editIndex_editPurchPlan);
			}
		}
	}

	//添加行
	function append_editPurchPlan(){
		if (endEditing_editPurchPlan()){
			//$('#yzr-editPurchPlan-dg').datagrid('appendRow');
			$('#yzr-editPurchPlan-dg').datagrid('appendRow',{
				'purchDetId': [],'medId': [], 
				'purchDetGuige': [], 'purchDetUnit': [],
				'purchDetNumber': [],'supId': [],
				'purchDetYijiMoney': []}); 
			
			
			editIndex_editPurchPlan = $('#yzr-editPurchPlan-dg').datagrid('getRows').length-1;
			$('#yzr-editPurchPlan-dg').datagrid('selectRow', editIndex_editPurchPlan)
					.datagrid('beginEdit', editIndex_editPurchPlan);
		}
	}
	
	//移除一行
	function removeit_editPurchPlan(){
		
		if (editIndex_editPurchPlan == undefined){return}
		
		$('#yzr-editPurchPlan-dg').datagrid('cancelEdit', editIndex_editPurchPlan)
				.datagrid('deleteRow', editIndex_editPurchPlan);
		
		editIndex_editPurchPlan = undefined;
	}

	
	//应用
	function accept_editPurchPlan(){
		if (endEditing_editPurchPlan()){
			$('#yzr-editPurchPlan-dg').datagrid('acceptChanges');
		}
	}
	
	
	//撤销
	function reject_editPurchPlan(){
		$('#yzr-editPurchPlan-dg').datagrid('rejectChanges');
		editIndex_editPurchPlan = undefined;
	}
	
	//提交采购计划
	function saveEditPurchPlanYzr(){
		var rows = $('#yzr-editPurchPlan-dg').datagrid('getRows');		
		alert(rows.length);
		console.log(JSON.stringify(rows));
		var purchMakepeople = $("#yzr-editPurchPlan-mkp").combogrid("getValue");//制定人
		alert(purchMakepeople);
		for(var i = 0; i < rows.length; i ++){
			generateHiddenYzr(rows[i], "medId", i);
			generateHiddenYzr(rows[i], "medName", i);
			generateHiddenYzr(rows[i], "purchDetId", i);
			generateHiddenYzr(rows[i], "purchDetPurchId", i);
			generateHiddenYzr(rows[i], "purchDetGuige", i);
			generateHiddenYzr(rows[i], "purchDetUnit", i);
			generateHiddenYzr(rows[i], "purchDetNumber", i);
			generateHiddenYzr(rows[i], "supId", i);
			generateHiddenYzr(rows[i], "purchDetYijiMoney", i);
		}
		
		var ff_plan = $("#yzr_editPurchPlan_form");
		
		ff_plan.form("submit", {
			url: 'editPurchPlan',
			onSubmit: function(){
		    	return $(this).form("validate");
		    },
			success: function(v){
				$.messager.alert('提示', "提交成功");
				$(":hidden[x='y']").remove();			
				//$('#yzr_editPurchPlan_form').form("clear");
				//reject_editPurchPlan();
				$('#yzr-editPurchPlan-window').window('close');
				$("#tb-medPurchPlan").datagrid("reload");
			}
		});
	}
	
	
	//name=medPurchDets[0] .属性名称
	//value = row[属性名称]
	//创建隐式表单域来保存明细
	function generateHiddenYzr(row, name, i){
		var ff_plan = $("#yzr_editPurchPlan_form");
		$("<input type='hidden' x='y'>")
			.attr("name", "medPurchDets[" + i + '].'  + name)
			.val(row[name])
			.appendTo(ff_plan);
	}
	
	//结束编辑，同步数据
	/* function onEndEdit_editPurchPlan(index, row){
		
	} */
</script>

<script type="text/javascript">
	$(function(){
		
		//查看采购明细
		/* $("#yzr-medPurchPlan-detail").linkbutton({onClick: function(){
			var rows = $("#tb-medPurchPlan").datagrid("getSelections");
			if(rows.length == 1){			
				$('#yzr-editPurchPlan-window').window("open");
				var purchId = rows[0].purchId;
				$("#tb-medPurchPlanDet").datagrid({
		    		url: 'queryAllPurchDet', 
		    		method: 'post', 
		    		queryParams: {
		    			page:1, 
		    			rows:10,
		    			purchId: purchId	    			
		    		}});			
			}else{
				$.messager.alert("提示", "只能选择一条记录，您目前选择了" + rows.length + "条!");
			}
		}}); */
		
		
		
		//日期转换
		function yzr_todate(value){
            var date = new Date(value);  
            return date.toLocaleDateString();
        }
		
		//打开编辑采购计划窗口
		$("#yzr-medPurchPlan-detail").linkbutton({onClick: function(){
			var rows = $("#tb-medPurchPlan").datagrid("getSelections");
			if(rows.length == 1){
				$('#yzr-editPurchPlan-window').window({'title': "编辑采购计划"});
				$('#yzr-editPurchPlan-window').window("open");
				var purchId = rows[0].purchId;
				
				$.getJSON("queryMedPurchById", {"purchId": purchId}, function(data){
					$("#yzr-editPurchPlan-id").textbox("setValue",data.purchId);
					$("#yzr-editPurchPlan-title").textbox("setValue",data.purchTitle);
					$("#yzr-editPurchPlan-mkp").combogrid("setValue",data.purchMakepeople);
					$("#yzr-editPurchPlan-zdrq").datebox("setValue", yzr_todate(data.purchZdTime));//制定日期
					$("#yzr-editPurchPlan-spr").combogrid("setValue",  data.purchAutdit);					
					$("#yzr-editPurchPlan-dhrq").datebox("setValue",  yzr_todate(data.purchArriivalDate));//预计到货日期 				
				});	
				
				 //根据id查看详情	
				 $("#yzr-editPurchPlan-dg").datagrid({
					url:'queryAllPurchDet2',
					queryParams:{
						purchId: purchId
					}
				});
			}else{
				$.messager.alert("提示", "请选择一条记录!");
			}
		}});
	});
</script>

	<!-- 显示所有计划 -->
	<table id="tb-medPurchPlan" class="easyui-datagrid" 
		data-options="
			url:'queryAllMedPurch',
			singleSelect: true,
			fitColumns:true,
			fit:true,
			pagination: true,
			pageSize: 10,
			pageList: [10, 20, 30, 40],
			rownumbers: true,
			toolbar: '#yzr-medPurchPlan-tb',
			border:false
			">
		<thead>
			<tr>	
				<th data-options="field:'purchId',width:'10%',checkbox:true,align:'center'">计划编号</th>				
				<th data-options="field:'purchTitle',width:'17%',align:'center'">项目名称</th>
				<th data-options="field:'staffName',width:'11%',align:'center'">制定人</th>
				<th data-options="field:'staffNameSp',width:'11%',align:'center'">审批人</th>
				
				<th data-options="field:'purchZdTime',width:'16%',
									formatter:function(value){
					                          var date = new Date(value);  
					                          return date.toLocaleDateString();},align:'center'"
				>制定时间</th>
				
				<th data-options="field:'purchAutditState',width:'16%',align:'center'">审批状态</th>
				
				<th data-options="field:'purchArriivalDate',width:'16%',
									formatter:function(value){
					                          var date = new Date(value);  
					                          return date.toLocaleDateString();},align:'center'"
				>预计到货时间</th>
				<th data-options="field:'purchState',width:'11%',align:'center'">采购状态</th>
			</tr>
		</thead>
	</table>
	
	<!-- 工具栏 -->
	<div id="yzr-medPurchPlan-tb">
		<a id="yzr-medPurchPlan-detail" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改计划</a>		
	</div>
	
	<!--修改窗口-->
	<div id="yzr-editPurchPlan-window" class="easyui-window" title="打印采购详情" data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true" style="width:70%;height:70%;">		
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">						
			<div data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">				
				<form id="yzr_editPurchPlan_form" method="get">
				
					<table width="100%">
						<tr>
							<td width="33%">
								<input class="easyui-textbox" readonly name="purchId" id="yzr-editPurchPlan-id" data-options="label: '计划单号',width:'90%'"/>
							</td>
							<td width="33%">
								<input class="easyui-textbox" name="purchTitle" id="yzr-editPurchPlan-title" data-options="required:true,label: '项目名称',width:'90%'"/>
							</td>
							<td  width="33%">
								<select class="easyui-combogrid" id="yzr-editPurchPlan-mkp" name="purchMakepeople"
									data-options="
										label: '制定人',
										width: '80%',
										required:true,
										panelWidth: '290',
										panelHeight: 'auto',
										url: 'queryAllStaff',
										pagination: true,
										pageSize: 8,
										pageList: [8, 16, 24, 32],
										idField: 'staffNo',
										textField: 'staffName',
										columns: [[
											{field:'staffName',title:'姓名',width:100},
											{field:'departmentName',title:'部门',width:100},
											{field:'postName',title:'职务',width:85}
										]]
									"></select>
							</td>
						</tr>
						<tr>
							<td>
								<select class="easyui-combogrid" id="yzr-editPurchPlan-spr" name="purchAutdit"
									data-options="
										label: '审批人',
										width: '80%',
										required:true,
										panelWidth: '290',
										panelHeight: 'auto',
										url: 'queryAllStaff',
										pagination: true,
										pageSize: 8,
										pageList: [8, 16, 24, 32],
										idField: 'staffNo',
										textField: 'staffName',
										columns: [[
											{field:'staffName',title:'姓名',width:100},
											{field:'departmentName',title:'部门',width:100},
											{field:'postName',title:'职务',width:85}
										]]
									"></select>
							</td>
							<td>
								<input class="easyui-datebox" name="purchArriivalDate" id="yzr-editPurchPlan-dhrq"
									data-options="label:'预计到货日期',width:'80%'">
							</td>
							<td>
								<input class="easyui-datebox" name="purchZdTime" id="yzr-editPurchPlan-zdrq"
									data-options="label:'制定日期',width:'80%'">
							</td>
						</tr>
					</table>
						
						
					<div style="margin: 5px;">
						<div id="yzr-editPurchPlan-tabs" class="easyui-tabs" style="width:100%; margin-top:10px;" data-options="plain:true,narrow:true">							
							<div title="采购计划明细" style="padding:10px;">
								<table id="yzr-editPurchPlan-dg" class="easyui-datagrid" 
										data-options="
											iconCls: 'icon-edit',
											singleSelect: true,		
											toolbar: '#yzr-editPurchPlan-tb',	
											onClickRow: onClickRow_editPurchPlan,												
											width: '100%'
											">
										<thead>
											<tr>	
												<th data-options="field:'purchDetId',width:'20%',align:'center'">计划明细编号</th>	
												<th data-options="field:'medId',width:'20%',align:'center',
														formatter:function(value,row){
															return row.medName;
														},
														editor:{
															type:'combogrid',
															options:{
																required:true,
																url: 'queryAllMedicine',
																pagination: true,
																pageSize: 8,
																pageList: [8, 16, 24, 32],
																idField: 'medId',
																textField: 'medName',
																panelWidth: '290',
																panelHeight: 'auto',
																columns: [[
																	{field:'medId',title:'药品编号',width:120},
																	{field:'medName',title:'药品名称',width:105},
																	{field:'medKind',title:'类型',width:60}
																]]										
															}
												}">药品名称</th>
												<th data-options="field:'purchDetGuige',width:'10%',align:'center',editor:{type:'textbox',options:{required:true}}">规格</th>
												<th data-options="field:'purchDetUnit',width:'10%',align:'center',editor:{
													type: 'combobox',
													options:{
														required:true,
														valueField: 'label',
														textField: 'value',
														panelHeight: 'auto',
														data: [{label:'箱',value:'箱'},
																{label:'盒',value:'盒'},
																{label:'瓶',value:'瓶'},
																{label:'袋',value:'袋'},
																{label:'片',value:'片'},
																{label:'支',value:'支'},
																{label:'板',value:'板'}
															]
													}
												}">单位</th>
												<th data-options="field:'purchDetNumber',width:'10%',align:'center',editor:{type:'numberbox',options:{required:true}}">数量</th>
												<th data-options="field:'supId',width:'20%',align:'center',
														formatter:function(value,row){
															return row.supName;
														},
														editor:{
															type: 'combogrid',								
															options:{
																url: 'queryAllSupplier',
																required:true,
																pagination: true,
																pageSize: 8,
																pageList: [8, 16, 24, 32],
																idField: 'supId',
																textField: 'supName',
																panelWidth: '290',
																panelHeight: 'auto',
																columns: [[
																	{field:'supId',title:'供应商编号',width:120},
																	{field:'supName',title:'供应商名称',width:105},
																	{field:'supKind',title:'类型',width:60}
																]]										
														}
													}">供应商</th>
												<th data-options="field:'purchDetYijiMoney',width:'11%',align:'center',editor:{type: 'numberbox',options:{precision:2,required:true}}">预计金额</th>
											</tr>
										</thead>
								 </table>
								 
								 <div id="yzr-editPurchPlan-tb" style="height:auto">
									<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append_editPurchPlan()">添加</a>
									<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit_editPurchPlan()">删除</a>
									<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept_editPurchPlan()">应用</a>
									<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject_editPurchPlan()">撤销</a>
								</div>
							</div>							
						</div>						
					</div>
				</form>				
			</div>					
					
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok', plain:true, onClick: saveEditPurchPlanYzr">保存</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#yzr-editPurchPlan-window').window('close');" onclick="">取消</a>
			</div>						
		</div>
	</div>
</body>
</html>